package com.example.weather_app.diary;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.weather_app.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

public class DiaryAdapter extends RecyclerView.Adapter<DiaryAdapter.DiaryViewHolder> {
    private Context context;
    private List<Diary> diaries;
    private SimpleDateFormat dateFormat, timeFormat;

    public DiaryAdapter(Context context) {
        this.context = context;
        this.dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        this.timeFormat = new SimpleDateFormat("HH:mm", Locale.CHINA);
    }

    public void setDiaries(List<Diary> diaries) {
        this.diaries = diaries;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public DiaryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.diary_item, parent, false);
        return new DiaryViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DiaryViewHolder holder, int position) {
        Diary diary = diaries.get(position);
        holder.bind(diary);
    }

    @Override
    public int getItemCount() {
        return diaries != null ? diaries.size() : 0;
    }

    class DiaryViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView tvTitle, tvContent, tvDate, tvReminder;

        DiaryViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvContent = itemView.findViewById(R.id.tvContent);
            tvDate = itemView.findViewById(R.id.tvDate);
            tvReminder = itemView.findViewById(R.id.tvReminder);
            itemView.setOnClickListener(this);
        }

        void bind(Diary diary) {
            tvTitle.setText(diary.getTitle());
            tvContent.setText(diary.getContent());
            tvDate.setText("日期: " + diary.getDate());

            // 处理提醒时间
            if (diary.getReminderTime() != null && !diary.getReminderTime().isEmpty()) {
                try {
                    SimpleDateFormat fullDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
                    java.util.Date reminderDate = fullDateFormat.parse(diary.getReminderTime());
                    String time = timeFormat.format(reminderDate);
                    tvReminder.setText("提醒: " + time);
                    tvReminder.setVisibility(View.VISIBLE);
                } catch (ParseException e) {
                    tvReminder.setVisibility(View.GONE);
                    e.printStackTrace();
                }
            } else {
                tvReminder.setVisibility(View.GONE);
            }
        }

        @Override
        public void onClick(View v) {
            int position = getAdapterPosition();
            if (position != RecyclerView.NO_POSITION) {
                Diary diary = diaries.get(position);
                Intent intent = new Intent(context, DiaryDetailActivity.class);
                intent.putExtra("diaryId", diary.getId());
                context.startActivity(intent);
            }
        }
    }
}